package com.yyfax.ph.home.imageupload;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;

import java.util.Map;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Header;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import rx.Observable;

public class UploadImageModel extends Model {

	private static final String TAG = "UploadImageModel";

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class, 30);//设置超时时间
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.uploadImage(token, ((UploadImage) params).type, ((UploadImage) params).part);
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		UploadResult result = cast(obj);
		EventAgent.post(result);
	}

	public interface Service {
		@Multipart
		@POST("sys/upload")
		Observable<UploadResult> uploadImage(@Header("token") String token, @Part("type") RequestBody type, @Part MultipartBody.Part file);
	}

	//todo 考虑把functionalCode放到参数类里
	public static class UploadImage {
		//需要上传的文件
		MultipartBody.Part part;
		RequestBody type;

		UploadImage(MultipartBody.Part part, RequestBody type) {
			this.part = part;
			this.type = type;
		}
	}

	static class UploadResult extends BaseResultEntity<Map<String, String>> {
	}

	static class UploadFailure {
		String filePath;
		String tips;
		String requestPage;
	}


	static class UploadCancel {
		String filePath;
		String tips;
		String requestPage;
	}
}
